package wec_wallet.ebo.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBassAppCompatActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.model.GsonModel;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.activity.options.ChangePhoneActivity;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.URLs;

import static wec_wallet.ebo.com.activity.main.MineFragment.CODE_REFRESH;
import static wec_wallet.ebo.com.activity.main.MineFragment.CODE_REQUEST;


/**
 * Created by admin on 2018/3/30.
 */

public class SafeLevelActivity extends MyBassAppCompatActivity {

    @BindView(R.id.tv_lv1_status)
    TextView tvLv1Status;
    @BindView(R.id.tv_lv2_status)
    TextView tvLv2Status;
    @BindView(R.id.tv_lv3_status)
    TextView tvLv3Status;
    @BindView(R.id.tv_lv4_status)
    TextView tvLv4Status;
    @BindView(R.id.tv_lv5_status)
    TextView tvLv5Status;
    @BindView(R.id.tv_lv1)
    TextView tvLv1;
    @BindView(R.id.tv_24_lv1)
    TextView tv24Lv1;
    @BindView(R.id.tv_lv2)
    TextView tvLv2;
    @BindView(R.id.tv_24_lv2)
    TextView tv24Lv2;
    @BindView(R.id.tv_lv3)
    TextView tvLv3;
    @BindView(R.id.tv_24_lv3)
    TextView tv24Lv3;
    @BindView(R.id.tv_lv4)
    TextView tvLv4;
    @BindView(R.id.tv_24_lv4)
    TextView tv24Lv4;
    @BindView(R.id.tv_lv5)
    TextView tvLv5;
    @BindView(R.id.tv_24_lv5)
    TextView tv24Lv5;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_child_lv1)
    TextView tvChildLv1;
    @BindView(R.id.tv_child_lv2)
    TextView tvChildLv2;
    @BindView(R.id.tv_child_lv3)
    TextView tvChildLv3;
    @BindView(R.id.tv_child_lv4)
    TextView tvChildLv4;
    @BindView(R.id.tv_child_lv5)
    TextView tvChildLv5;


    private List<ViewObject> mList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_verify);
        ButterKnife.bind(this);
        mList.add(new ViewObject(tvLv1Status, tvLv1, tv24Lv1, tvChildLv1));
        mList.add(new ViewObject(tvLv2Status, tvLv2, tv24Lv2, tvChildLv2));
        mList.add(new ViewObject(tvLv3Status, tvLv3, tv24Lv3, tvChildLv3));
        mList.add(new ViewObject(tvLv4Status, tvLv4, tv24Lv4, tvChildLv4));
        mList.add(new ViewObject(tvLv5Status, tvLv5, tv24Lv5, tvChildLv5));

        getUserInfo();

    }

    @OnClick(R.id.btn_vfy_telephone)
    public void onBtnVfyTelephoneClicked() {
        startActivityForResult(new Intent(this, ChangePhoneActivity.class), CODE_REQUEST);
    }

    @OnClick(R.id.btn_vfy_mail)
    public void onBtnVfyMailClicked() {
        startActivityForResult(new Intent(this, MailVerificationActivity.class), CODE_REQUEST);
    }

    @OnClick(R.id.btn_vfy_identity)
    public void onBtnVfyIdentityClicked() {
        IToast.show(getString(R.string.no_open));
//        startActivityForResult(new Intent(this, IdentityVerificationActivity.class), CODE_REQUEST);
    }

    //返回
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        finish();
    }


    private void getLevelInfo() {
        dismissLoading();
        if (true) return;


        if (I_Share.isLogin()) {
            Map<String, Object> postParams = new HashMap<>();
            postParams.put("token", I_Share.readAccountToken());
            postParams.put("uid", I_Share.readAccountUserid());
            Functions.post(this, URLs.GET_LEVEL_INFO, postParams, new IHttpPostListener() {
                @Override
                public void onTimeOut() {

                }
                @Override
                public void responseSuccess(String result) {
                    Lg.d(result);
                    if (!TextUtils.isEmpty(result)) {
                        Type type = new TypeToken<Resp>() {
                        }.getType();
                        Resp resp = gson.fromJson(result, type);
                        if (resp != null && "0".equalsIgnoreCase(resp.getError())) {
                            onPostExcute(resp.getData());
                        }
                    }
                    dismissLoading();
                }
            }, false, false);
        } else {
            dismissLoading();
            finish();
        }
    }


    private void getUserInfo() {
        if (I_Share.isLogin()) {
            showLoading();
            Map<String, Object> postParams = new HashMap<>();
            postParams.put("token", I_Share.readAccountToken());
            postParams.put("uid", I_Share.readAccountUserid());
            Functions.post(this, URLs.GET_USER_INFO, postParams, new IHttpPostListener() {
                @Override
                public void onTimeOut() {

                }
                @Override
                public void responseSuccess(String result) {

                    if (!TextUtils.isEmpty(result)) {
                        Type type = new TypeToken<MineActivity.UserInfo>() {
                        }.getType();
                        MineActivity.UserInfo resp = gson.fromJson(result, type);
                        if (resp != null && "0".equalsIgnoreCase(resp.getError())) {


                            int green = getResources().getColor(R.color.design_color_green);
                            int red = getResources().getColor(R.color.design_color_red);
                            int gray = getResources().getColor(R.color.design_color_disable);

                            mList.get(0).status.setText(getString(R.string.already_open));
                            mList.get(0).status.setTextColor(green);

                            if (!TextUtils.isEmpty(resp.getEmail())) {
                                mList.get(1).status.setText(getString(R.string.already_open));
                                mList.get(1).status.setTextColor(green);
                            } else {
                                mList.get(1).status.setText(getString(R.string.unopened));
                                mList.get(1).status.setTextColor(red);
                            }
                            /*

                            if (resp.getIs_idcard() == 1) {
                                mList.get(2).status.setText(getString(R.string.already_open));
                                mList.get(2).status.setTextColor(green);
                            } else {
                                mList.get(2).status.setText(getString(R.string.unopened));
                                mList.get(2).status.setTextColor(red);
                            }*/
                            mList.get(2).status.setTextColor(gray);
                            mList.get(3).status.setTextColor(gray);
                            mList.get(4).status.setTextColor(gray);
                            tvLevel.setText("Lv." + resp.getLevel());

                        }
                    }
                    getLevelInfo();
                }
            }, false, false);
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //更新用户等级
        if (CODE_REFRESH == resultCode && requestCode == CODE_REQUEST) {
            Lg.d("getLevelInfo");
            getUserInfo();
            setResult(CODE_REFRESH);
        }
    }

    private void onPostExcute(List<Data> list) {
        for (int i = 0; i < list.size(); i++) {
            ViewObject o = mList.get(i);
            Data data = list.get(i);
            String level = getString(R.string.verification_level);
            String h24 = getString(R.string.h24_withdraw_deposit);

            level = String.format(level, data.getLevel() + "");
            h24 = String.format(h24, data.getQuota());
            if (data.getStatus() == 0) {
                o.lv.setTextColor(getResources().getColor(R.color.design_color_disable));
                o.lv.setText(level + "(" + getString(R.string.yet_open) + ")");
                o.h24.setTextColor(getResources().getColor(R.color.design_color_disable));
            } else {
                o.lv.setText(level);
            }

//            o.h24.setText(h24);
            o.h24.setVisibility(View.GONE);


            if (i > 0) {
                o.clv.setText("Lv." + list.get(i - 1).getLevel() + "+");
            }



        }
    }


    class Resp extends GsonModel{
        private String error;
        private List<Data> data;

        public String getError() {
            return error;
        }

        public void setError(String error) {
            this.error = error;
        }

        public List<Data> getData() {
            return data;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }
    }

    class Data extends GsonModel{
        private int level;
        private String quota;
        private int status;

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public String getQuota() {
            return quota;
        }

        public void setQuota(String quota) {
            this.quota = quota;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }
    }

    private class ViewObject {


        TextView status;
        TextView lv;
        TextView h24;
        TextView clv;

        public ViewObject(TextView status, TextView lv, TextView h24, TextView clv) {
            this.status = status;
            this.lv = lv;
            this.h24 = h24;
            this.clv = clv;
        }
    }

}
